<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="close" @open="getSttingListFn">
    <!-- 双向绑定会显示的数据 -->
    <el-checkbox-group v-model="list" v-loading="isloding">
      <!-- 遍历获得的全部角色列表 -->
      <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id">    {{ item.name }}</el-checkbox>

      <!-- <el-checkbox label="复选框 B" />
      <el-checkbox label="复选框 C" /> -->
      <!-- <el-checkbox label="禁用" disabled /> -->
      <!-- <el-checkbox label="选中且禁用" disabled /> -->
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="editFn">确定</el-button>
      <el-button size="small">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getSttingList, editROleSttingApi } from '@/api/setting'
import { getUserInfoByIdApi } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [],
      list: [],
      isloding: false
    }
  },
  created() {
    // this.getSttingListFn()
  },
  methods: {
    close() {
      this.$emit('update:showRoleDialog', false)
    },

    async getSttingListFn() {
      this.isloding = true
      // 传递必须的参数，没有新接口，所以这样写
      const res = await getSttingList(1, 100)
      this.checkList = res.data.rows
      console.log(this.userId, '5555')
      const res2 = await getUserInfoByIdApi(this.userId)
      this.list = res2.data.roleIds
      this.isloding = false
      // this.checkList.name = res.data.rows.name
    },
    async editFn() {
      await editROleSttingApi({ id: this.userId, roleIds: this.list })
      this.$message.success('修改成功')
      this.close()
    }
  }
}
</script>
